﻿using UnityEngine;
using System.Collections;
using System.Text;

namespace UnityEngine.UI
{
    public class UIHelper
    {
        public static bool isEmojiCharacter(int codePoint)
        {
            return (codePoint >= 0x2600 && codePoint <= 0x27BF) // 杂项符号与符号字体
                    || codePoint == 0x303D  //中日韩统一符号(CJK)
                    || codePoint == 0x2049  //通用符号
                    || codePoint == 0x203C

                    //添加
                    || codePoint == 0x00A9 || codePoint == 0x00AE   //拉丁符号
                    || IsIn(codePoint, 0x2194, 0x2199)              //箭头
                    || IsIn(codePoint, 0x21A9, 0x21AA)              //箭头
                    || IsIn(codePoint, 0x25AA, 0x25AB)              //几何图形
                    || IsIn(codePoint, 0x25FB, 0x25FE)              //几何图形
                    || codePoint == 0x25B6 || codePoint == 0x25C0   //几何图形
                    || codePoint == 0x3030   //中日韩统一符号(CJK)
                                             //End

                    || IsIn(codePoint, 0x2000, 0x200F)
                    || IsIn(codePoint, 0x2028, 0x202F)
                    || codePoint == 0x205F
                    || IsIn(codePoint, 0x2065, 0x206F)
                    || IsIn(codePoint, 0x2100, 0x214F)              // 字母符号
                    || IsIn(codePoint, 0x2300, 0x23FF)              //各种技术符号
                    || IsIn(codePoint, 0x2B00, 0x2BFF)              // 箭头A
                    || IsIn(codePoint, 0x2900, 0x297F)              // 箭头B
                    || IsIn(codePoint, 0x3200, 0x32FF)              // 中文符号
                    || IsIn(codePoint, 0xD800, 0xDFFF)              // 高低位替代符保留区域
                    || IsIn(codePoint, 0xE000, 0xF8FF)              // 私有保留区域
                    || IsIn(codePoint, 0xFE00, 0xFE0F)              // 变异选择器
                    || codePoint >= 0x10000; // Plane在第二平面以上的，char都不可以存，全部都转
                                             //|| codePoint == 0x3297 || codePoint == 0x3299   //中日韩统一字母(CJK)
        }

        public static string FormatStringNoEmoji(string str)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < str.Length; i++)
            {
                if (isEmojiCharacter(str[i])) continue;
                sb.Append(str[i]);
            }
            return sb.ToString();
        }

        public static bool IsIn(int codePoint, int min, int max)
        {
            return codePoint >= min && codePoint <= max;
        }

        /// <summary>
        /// 根据组件宽度裁剪文本，并添加后缀
        /// 如：hello world=>hello wor...
        /// </summary>
        /// <param name="text">文本UI组件</param>
        /// <param name="txt">文本</param>
        /// <param name="suffix">后缀</param>
        /// <returns></returns>
        public static string ClampText(Text text, string txt, string suffix = null)
        {
            GUIStyle style = GUIStyle.none;
            style.fontSize = text.fontSize;//设置字体大小
            float maxWidth = text.rectTransform.sizeDelta.x - 10;//获取最大宽度(减10为不同分辨率导致的误差)

            float fontWidth = GetWidth(txt, style);//当前文本宽度
            if (fontWidth > maxWidth)
            {
                //获取后缀宽度
                float suffixWidth = GetWidth(suffix, style);
                maxWidth -= suffixWidth;
                //计算平均宽度
                float unitWidth = GetUnitWidth(txt, style);
                int count = (int)((fontWidth - maxWidth) / unitWidth);

                StringBuilder sb = new StringBuilder(txt);
                sb.Remove(0, count);
                fontWidth = GetWidth(sb.ToString(), style);

                if (fontWidth > maxWidth)
                {//递减
                    while (fontWidth > maxWidth)
                    {
                        sb.Remove(0, 1);
                        fontWidth = GetWidth(sb.ToString(), style);
                        //Debug.Log(string.Format("-[{0:f1}] {1}", fontWidth, sb.ToString()));
                    }
                }
                else
                {//递增
                    int offset = count;
                    while (fontWidth < maxWidth && offset > 0)
                    {
                        offset--;
                        sb.Insert(0, txt[offset]);
                        fontWidth = GetWidth(sb.ToString(), style);
                        //Debug.Log(string.Format("+[{0:f1}] {1}", fontWidth, sb.ToString()));
                    }
                    sb.Remove(0, 1);
                }
                txt = suffix + sb.ToString();
            }
            return txt;
        }

        static float GetUnitWidth(string text, GUIStyle style)
        {
            StringBuilder sb = new StringBuilder();
            int length = text.Length;
            int count = 10;
            for (int i = 0; i < count; i++)
                sb.Append(text[Random.Range(0, length)]);
            return style.CalcSize(new GUIContent(text)).x / count;
        }

        static float GetWidth(string text, GUIStyle style)
        {
            if (style == null || string.IsNullOrEmpty(text)) return 0;
            return style.CalcSize(new GUIContent(text)).x;
        }

        public static string SetColor(Text text, string txt, string color)
        {
            if (!string.IsNullOrEmpty(txt) && !string.IsNullOrEmpty(color))
            {
                if (text.text.IndexOf(txt) >= 0)
                {
                    StringBuilder sb = new StringBuilder(text.text);
                    sb.Replace(txt, string.Format("<color={0}>{1}</color>", color, txt));
                    return sb.ToString();
                }
            }
            return null;
        }

        public static string SetColor(string srcText, string txt, string color)
        {
            if (!string.IsNullOrEmpty(txt) && !string.IsNullOrEmpty(color))
            {
                if (srcText.IndexOf(txt) >= 0)
                {
                    StringBuilder sb = new StringBuilder(srcText);
                    sb.Replace(txt, string.Format("<color={0}>{1}</color>", color, txt));
                    return sb.ToString();
                }
            }
            return null;
        }

        public static string SetLink(Text text, string txt, string color, string herf)
        {
            if (!string.IsNullOrEmpty(txt) && !string.IsNullOrEmpty(color))
            {
                if (text.text.IndexOf(txt) >= 0)
                {
                    StringBuilder sb = new StringBuilder(text.text);
                    sb.Replace(txt, string.Format("<a href={2} color={1}>{0}</a>", txt, color, herf));
                    return sb.ToString();
                }
            }
            return null;
        }
    }
}
